<template>
    <view class="coupon">
        <view class="coupon_item" v-for="(item, index) in canUseList" :key="item.id" @click="handlerSelectIndex(index)">
            <image class="bg" src="@/static/images/other/coupon_bg.png" />
            <view class="coupon_content">
                <view class="coupon_money">
                    <text class="unit">¥</text>
                    <text class="money">{{ item.discountPrice }}</text>
                </view>
                <view class="coupon_tips">
                    <view class="tips">{{ `满${item.usePrice}可用` }}</view>
                    <view class="use">有效期至: {{ getTime(item.validEndTime) }}</view>
                </view>
                <view class="coupon_select">
                    <image class="icon" v-if="index == selectIndex" src="@/static/images/other/icon_select.png" />
                    <image class="icon" v-else src="@/static/images/other/icon_unselect.png" />
                </view>
            </view>
        </view>
        <view class="coupon_item disabled" v-for="item in cantUseList" :key="item.id">
            <image class="bg" src="@/static/images/other/coupon_bg.png" />
            <view class="coupon_content">
                <view class="coupon_money">
                    <text class="unit">¥</text>
                    <text class="money">{{ item.discountPrice }}</text>
                </view>
                <view class="coupon_tips">
                    <view class="tips">{{ `满${item.usePrice}可用` }}</view>
                    <view class="use">有效期至: {{ getTime(item.validEndTime) }}</view>
                </view>
            </view>
        </view>
        <!-- 底部按钮 -->
        <view class="bottom_btn">
            <view class="btn flex_c" @click="submit">确定使用</view>
        </view>
    </view>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { getCouponPage } from '@/api/other/coupon';
import { onLoad } from '@dcloudio/uni-app';
import { getTime } from '@/utils';

// 订单支付金额
const money = ref(99999);
onLoad((options: any) => {
    if (options.money) {
        money.value = Number(options.money)
    }
    getCoupon()
})

// 查询优惠券列表
const canUseList = ref<any>([]); // 可用优惠券
const cantUseList = ref<any>([]); // 不可用优惠券
const getCoupon = () => {
    const par = {
        status: 1
    }
    getCouponPage(par.status).then((res: any) => {
        if (res.code == 0 && res.data) {
            const list = res.data.list
            list.forEach((item: any) => {
                item.discountPrice = item.discountPrice / 100
                item.usePrice = item.usePrice / 100
                if (item.usePrice <= money.value) {
                    canUseList.value.push(item)

                } else {
                    cantUseList.value.push(item)
                }
            })
        }
    })
}

const selectIndex = ref<number>(0);
const handlerSelectIndex = (index: number) => {
    selectIndex.value = index;
}

const submit = () => {
    const data = canUseList.value[selectIndex.value]
    uni.navigateBack({
        delta: 1,
        success: () => {
            uni.$emit('selectCoupon', data)
        }
    })
}
</script>

<style lang="scss" scoped>
.coupon {
    min-height: 100vh;
    overflow: hidden;
    background: #F8F8F8;
    padding-bottom: 200rpx;
    box-sizing: border-box;
}

.coupon_item {
    width: 698rpx;
    height: 152rpx;
    position: relative;
    margin: 0 auto;
    margin-top: 24rpx;
}

.bg {
    width: 698rpx;
    height: 152rpx;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 1;
}

.coupon_content {
    width: 698rpx;
    height: 152rpx;
    position: absolute;
    top: 0;
    left: 0;
    z-index: 2;
    display: flex;
}

.coupon_money {
    width: 160rpx;
    height: 152rpx;
    line-height: 152rpx;
    font-family: 'DIN';
    font-weight: bold;
    color: #ffffff;
    display: flex;
    align-items: baseline;
    justify-content: center;

    .unit {
        font-size: 32rpx;
        margin-right: 8rpx;
    }

    .money {
        font-size: 64rpx;
    }
}

.coupon_tips {
    margin-left: 24rpx;

    .tips {
        font-weight: 700;
        font-size: 32rpx;
        color: #222222;
        margin-top: 24rpx;
    }

    .use {
        font-size: 26rpx;
        color: #888888;
        margin-top: 24rpx;
    }
}

.coupon_select {
    position: absolute;
    top: 56rpx;
    right: 32rpx;

    .icon {
        width: 40rpx;
        height: 40rpx;
        border-radius: 50%;
    }
}

.disabled {
    opacity: 0.6;
}

.bottom_btn {
    width: 750rpx;
    height: 198rpx;
    background: #ffffff;
    position: fixed;
    bottom: 0;
    z-index: 99;

    .btn {
        width: 650rpx;
        height: 80rpx;
        background: #fe6e04;
        border-radius: 16rpx 16rpx 16rpx 16rpx;
        font-weight: 600;
        font-size: 32rpx;
        color: #ffffff;
        margin: 0 auto;
        margin-top: 36rpx;
    }
}
</style>